from common.base import Base, By
from common.functions import host, browser_operation


class IndexPage(Base):
    # 主页路径
    index_url = host

    def font_text(self):
        # 获取登陆后用户名
        return self.get_element_text((By.CLASS_NAME, 'f4_b'))

    def logout(self):
        # 登陆后退出
        self.click((By.LINK_TEXT, '退出'))

    def reg_btn(self):
        # 定位到注册按钮点击
        self.click((By.LINK_TEXT, '免费注册'))

    def home_btn(self):
        # 定位回到首页按钮点击
        self.click((By.XPATH, '/html/body/div[2]/div/div[1]/a/img'))

    def shopcart_btn(self):
        # 定位查看购物车按钮点击
        self.click((By.XPATH, '//*[@id="ECS_CARTINFO"]/a'))

    def category_url_list(self):
        """获取频道种类的链接href属性"""

        # 定位频道种类树中的所有类目的位置
        category_locator = (By.CSS_SELECTOR, '.cat-box>.cat1>a')
        # 通过定位获取类目元素(列表)
        a_elements = self.find_elements(category_locator)
        # 返回类目元素的href属性值
        return [a_element.get_attribute('href') for a_element in a_elements]


if __name__ == '__main__':
    driver = browser_operation()
    index = IndexPage(driver)
    index.get_website(host)
    print(index.category_url_list())
